/*
	fault.c
*/
#include <stdint.h>
#include "kernel.h"
#include "iroute.h"
#include "fault.h"
#include "mt.h"

char *____fault_lmsg[] = {
	"Divide Error",
	"Debug Exception",
	"Non-maskable",
	"Breakpoint",
	"Overflow",
	"Bounds",
	"Invalid Opcode",
	"Device Does Not Exist",
	"Double Fault",
	"Segment Overun",
	"Invalid TSS",
	"Segment Not Present",
	"Stack Fault",
	"General Purpose",
	"Page Fault",
	"Unknown",
	"8087 Floating Point",
	"Alignment Check",
	"Machine Check",
	"SIMD Floating Point"};

void fault_handler(uint32_t line)
{
	sched_stackTemplate *st = (sched_stackTemplate*)____sched_curthread->ustack;
	kdbgerror("fault: %s\n", ____fault_lmsg[line]);
	kdbgerror("EAX:%08x ECX:%08x EDX:%08x EBX:%08x\n", st->eax, st->ecx, st->edx, st->ebx);
	kdbgerror("EDI:%08x ESI:%08x EBP:%08x ESP:%08x\n", st->edi, st->esi, st->ebp, st->esp);
	kdbgerror("EIP:%08x  CS:%08x EFLAGS:%08x\n", st->eip, st->cs, st->eflags);
	kdbgerror("CTHREAD:%08x\n", ____sched_curthread);
	sched_thread_terminate(sched_thread_current());
	sched_switch_core();
	return;
}

void fault_init()
{
	uint32_t x;
	for(x = 0; x < 20; ++x)
	{
		iroute_makehook(x, (uint32_t)&fault_handler);
	}
	return;
}